package com.afishing.servlet;

import com.afishing.entity.User;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "LogOutServlet", value = "/logout")
public class LogOutServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletContext application=this.getServletContext();
        HttpSession session=request.getSession();
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        User user = (User) session.getAttribute("user");
        ArrayList users = (ArrayList) application.getAttribute("users");
        users.remove(user);
        //下线把用户session销毁

        ArrayList msgs = (ArrayList)application.getAttribute("msgs");
        msgs.add(user.getUsername() + "下线啦!");
        request.getSession().invalidate();
        response.sendRedirect("index.jsp");

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doGet(request, response);
    }
}
